package com.example.administrator.newfragmentation.fragment.simple;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.fragmentMain.BaseBlankFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterFragment extends BaseBlankFragment {

    @Bind(R.id.et_account)
    EditText etAccount;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.et_password_confirm)
    EditText etPasswordConfirm;
    @Bind(R.id.btn_register)
    Button btnRegister;
    private LoginFragment.OnLoginSuccessListener listener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof LoginFragment.OnLoginSuccessListener) {
            listener = (LoginFragment.OnLoginSuccessListener) activity;
        } else {
            throw new RuntimeException(activity.toString()
                    + " must implement OnLoginSuccessListener");
        }
    }

    public static RegisterFragment newInstance() {
        RegisterFragment fragment = new RegisterFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.e("asd","asdasds");
        View view = inflater.inflate(R.layout.fragment_register, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    @OnClick(R.id.btn_register)
    public void onClick() {
        String name = etAccount.getText().toString().trim();
        String pwd1 = etPassword.getText().toString().trim();
        String pwd2 = etPasswordConfirm.getText().toString().trim();
        if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd2)){
            Toast.makeText(_mActivity,"请输入完整信息",Toast.LENGTH_SHORT).show();
            return;
        }
        listener.onLoginSuccess(name);
        hideSoftInput();
        // 出栈某一个Fragment栈内之上的Fragments
        //false时，LoginFragment不会消失，true才会消失,LoginFragment为上个fragment
        popTo(LoginFragment.class,true);
    }
}
